package com.pay.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pay.common.pay.constant.Constant;
import com.pay.common.pay.utils.SnowflakeId;
import com.pay.entity.PayMerchantInfoDO;
import com.pay.mapper.PayMerchantInfoMapper;
import com.pay.service.PayMerchantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouyihe
 */
@Service
public class PayMerchantInfoServiceImpl implements PayMerchantInfoService {
    @Autowired
    private PayMerchantInfoMapper payMerchantInfoMapper;
    
    
    /**
     * 新增企业入驻信息---默认状态是未审核
     *
     * @param payMerchantInfo
     * @return
     */
    @Override
    @Transactional
    public boolean addPayMerchantInfo(PayMerchantInfoDO payMerchantInfo) {
        // 默认是未审核状态
        payMerchantInfo.setAuditStatus(Constant.MERCHANT_UNAPPROVED_STATUS);
        
        // 商家入驻生成商户id
        payMerchantInfo.setMerchantId(SnowflakeId.getIdStr());// 全局唯一 雪花算法
        
        return payMerchantInfoMapper.insert(payMerchantInfo) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }
    
    
    /**
     * 根据商户的社会信用代码获取商户信息(全局唯一的键),查询是否存在这个元素
     *
     * @param socialCreditCode
     * @return
     */
    @Override
    public PayMerchantInfoDO getBySocialCreditCodePayMerchantInfo(String socialCreditCode) {
        QueryWrapper<PayMerchantInfoDO> payMerchantInfoDOQueryWrapper = new QueryWrapper<>();
        payMerchantInfoDOQueryWrapper.eq("social_credit_code", socialCreditCode);
        PayMerchantInfoDO payMerchantInfo = payMerchantInfoMapper.selectOne(payMerchantInfoDOQueryWrapper);
        return payMerchantInfo;
    }
    
    /**
     * 根据商户的id获取商户信息(全局唯一的键)
     *
     * @param merchantId
     * @return
     */
    @Override
    public PayMerchantInfoDO getByMerchantIdInfo(String merchantId) {
        QueryWrapper<PayMerchantInfoDO> qw = new QueryWrapper<>();
        qw.eq("merchant_id", merchantId);
        PayMerchantInfoDO payMerchantInfo = payMerchantInfoMapper.selectOne(qw);
        return payMerchantInfo;
    }
}
